#ifndef _COMMON_FUNCTIONS_H_
#define _COMMON_FUNCTIONS_H_

#include <stdlib.h>
#include <time.h>

#include "../cuCommon/cuTypes.h"

static void rnInit(void)
{
  srand(clock());
}

static int32  rnInt32()
{
  return rand();
}

static int16 rnInt16()
{
  return rand() % 0xFFFF;
}

static int8  rnInt8()
{
  return rand() % 0xFF;
}

static float rnFloat(int32 mod = 0xFF)
{
  return (float)(rand() % mod) / (mod-1.0f);
}

static float rnsFloat(int32 mod = 0xFF)
{
  return rnFloat(mod) * 2.0f - 1.0f;
}

static float3 rnsFloat3(int32 mod = 0xFF)
{
  return f3(rnsFloat(mod), rnsFloat(mod), rnsFloat(mod));
}

static float4 rnsFloat4(int32 mod = 0xFF)
{
  return f4(rnsFloat(mod), rnsFloat(mod), rnsFloat(mod), rnsFloat(mod));
}

static double rnDouble(int32 mod = 0xFF)
{
  return (double)(rand() % mod) / (mod-1.0);
}

static float3 rnSphere(int32 mod = 0xFF)
{
  float3 r = {0, 0, 0};

  do 
  {
    r = rnsFloat3();
  } while(length(r) > 1.0);

  return r;
}

static float2 rnUnitSquare()
{
	float2 p;
	p.x = rnFloat();
	p.y = rnFloat();
	return p;
}

static float2 rnsUnitSquare()
{
	float2 p;
	p.x = rnsFloat();
	p.y = rnsFloat();
	return p;
}

static float2 rnsUnitDisk()
{
	float2 p;

	do {
		p = rnsUnitSquare();	
	} while (length(p) > 1.0f);

	return p;
}

#endif
